Search


📜 [專欄新文章] Ethereum Token — ERC20 mint 跟 burn...

  • Share this:


📜 [專欄新文章] Ethereum Token — ERC20 mint 跟 burn
✍️ Kimi Wu
📥 歡迎投稿: https://medium.com/taipei-ethereum-meetup #徵技術分享文 #使用心得 #教學文 #medium

這篇會著重在erc20 smart contract實作mint跟burn的部分,所以需要先了解erc20喔!

erc20 token在設計上可以a.預先產生,b.產生部分然後部分用挖的(mint)或是c.都用挖的。當然在應用上跟token屬性有關,例如是屬於security token或是utility token。簡單來說,像是股票可以獲利或是配息概念的算是security token,需要受各國法令監管,那utility token就比較像是點數的概念,至於細節怎麼分不是本篇重點。

mint的使用時機,例如在crowd sale時,每一筆進入crowd sale的錢,crowd sale 的contract 就會呼叫token contract去產生某個數量的token,這個動作就可以稱作mint。也有不用挖的,一開始就產生好,例如秘銀(MITH),從etherscan可以看到token contract怎麼寫的,這裡可以看到MITH的contract。當然你想產多少就多少,不過就看有沒有人買單 XD

回到正題,要”挖”聽起來,就覺得很複雜,但其實smart contract的本質就是記帳,所以其實就只是數字上的加減而已,下面是範例程式

function mint(address _to, uint256 _amount) public { totalSupply_ = totalSupply_.add(_amount); balances[_to] = balances[_to].add(_amount); emit Mint(_to, _amount); }

其實就只是把增加個人的token的數量,然後增加整個contract token的總數而已。當然,這種function需要做權限的控管,不然大家都可以自己產,token就會沒價值了。講完”挖”應該很好想像怎麼burn吧?! 就是把數量減掉就好了!(當然這也需要做權限控管)

function _burn(address _who, uint256 _value) public { balances[_who] = balances[_who].sub(_value); totalSupply_ = totalSupply_.sub(_value); emit Burn(_who, _value); }

這是基本的mint跟burn,至於在應用上有需要其他邏輯,就是基於這個在往上堆疊。例如可以多加Transfer的event(erc20 標準中的Transfer event),在mint時from就是0x0,burn就是to為0x0,在應用上如果要追蹤token數量,就會比較方便。

最後在分享openzeppelin-solidity這個github repository,在看網路上很多的sample code常會看到SafeMath或是Ownable這兩個contract,其實都是出自於openzeppelin,裡面還有很多contract的範例可以參考或是使用,最棒的是他們一直有根據最新solidity的版本而做更新,所以都會根據新的語法做更新。他們把module切得相當的細,非常的OO,不過在看的時候會比較難一口氣看完,這也是Vyper開發者覺得solidity需要改善的其中一個點 -「可讀性」。因為可讀性越差,可稽核性就越差,相較之下安全性就越差。

* Vyper是類Python語法的新一代smart contract語言,目前還在beta(0.1.0-beta.2),不過Casper的smart contract已經是用Vyper寫的囉!看來Ethereum foundation的開發者是傾向往這邊走。

有錯誤或是不同看法,歡迎指教喔!

Originally published at kimiwublog.blogspot.com.

Ethereum Token — ERC20 mint 跟 burn was originally published in Taipei Ethereum Meetup on Medium, where people are continuing the conversation by highlighting and responding to this story.

👏 歡迎轉載分享鼓掌


Tags:

About author
not provided
We have regular meeting twice per month on discussing blockchain technology, smart contracts and DApps development! We would love to have you join us!
View all posts